package com.bootdo.system.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.bootdo.common.utils.*;
import com.bootdo.system.domain.*;
import com.bootdo.system.service.*;
import com.bootdo.system.vo.BaojiaVo;
import com.bootdo.system.vo.ProjectVo;
import com.sun.org.apache.regexp.internal.RE;
import com.sun.tools.corba.se.idl.StringGen;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.apache.bcel.generic.RET;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 项目产品中间表
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2020-06-24 19:11:08
 */

@Controller
@RequestMapping("/system/productInner")
public class ProductInnerController {
    @Autowired
    private ProductInnerService productInnerService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private SonsysService sonsysService;
    @Autowired
    private ProductService productService;
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;


    public List<Integer> getSonsysListByProjectId(Integer projectId) {
        return productInnerService.getSonsysListByProjectId(projectId);
    }

    public List<ProductInnerDO> getSonsysExcelVoBySonsysId(Integer sonsysId) {

        List<ProductInnerDO> proDuctBysonsysId = productInnerService.getProDuctBysonsysId(sonsysId);

        return proDuctBysonsysId;

    }

    public List<ProjectVo> getVoBydo(List<ProductInnerDO> productInnerList) {
        ArrayList<ProjectVo> projectVos = new ArrayList<>();

        for (ProductInnerDO productInnerDO : productInnerList) {
            ProjectVo projectVo = new ProjectVo();
            ProductDO productDO2 = productService.get(productInnerDO.getProductId());
            productInnerDO.setProductDO(productDO2);

            SonsysDO sonsysDO = sonsysService.get(productInnerDO.getSonsysId());
            productInnerDO.setSonsysDO(sonsysDO);

            BeanUtils.copyProperties(productDO2, projectVo);
            BeanUtils.copyProperties(productInnerDO, projectVo);

            CategoryDO categoryDO = categoryService.get(productDO2.getCategoryId());
            projectVo.setCategoryName(categoryDO.getCategoryName());

            projectVo.setShebeiName(productInnerDO.getProductDO().getShebeiName());
            projectVo.setAnzhuangChengben(productInnerDO.getProductDO().getAnzhuangChengben());
            projectVo.setShebeiChengben(productInnerDO.getProductDO().getShebeiChengben());
            projectVo.setChengben(productInnerDO.getProductDO().getChengben());
            projectVo.setFacePrice(productInnerDO.getProductDO().getFacePrice());
            projectVo.setSonsysName(productInnerDO.getSonsysDO().getSonsysName());
            projectVos.add(projectVo);
        }

        return projectVos;
    }

    public ExcelWriter getExcelWriterALL() {
        ExcelWriter writer = ExcelUtil.getWriter();
        //自定义标题别名
        writer.addHeaderAlias("productNum", "产品编号");
        writer.addHeaderAlias("shebeiName", "产品名称");

        writer.addHeaderAlias("categoryName", "产品分类");
        writer.addHeaderAlias("baseParam", "基本参数");
        writer.addHeaderAlias("allParam", "完整参数");
        writer.addHeaderAlias("unit", "单位");
        writer.addHeaderAlias("brand", "品牌");
        writer.addHeaderAlias("xinghao", "型号");


        writer.addHeaderAlias("sonsysName", "子系统名字");
        writer.addHeaderAlias("anzhuangChengben", "人工成本");
        writer.addHeaderAlias("shebeiChengben", "机器成本");
        writer.addHeaderAlias("chengben", "材料成本");
        writer.addHeaderAlias("facePrice", "面价");
        writer.addHeaderAlias("baojia", "报价");
        writer.addHeaderAlias("zhekou", "折扣");
        writer.addHeaderAlias("shijibaojia", "实际报价");
        writer.addHeaderAlias("proNum", "数量");
        writer.addHeaderAlias("allchengben", "总成本");
        writer.addHeaderAlias("allbaojia", "总报价");
        writer.addHeaderAlias("alllirun", "总利润");
        writer.addHeaderAlias("profit", "利润比(%)");
        writer.addHeaderAlias("beizhu", "备注");

        return writer;
    }

    public ExcelWriter getExcelWriterALLNochengben() {
        ExcelWriter writer = ExcelUtil.getWriter();
        //自定义标题别名
        writer.addHeaderAlias("productNum", "产品编号");
        writer.addHeaderAlias("shebeiName", "产品名称");

        writer.addHeaderAlias("categoryName", "产品分类");
        writer.addHeaderAlias("baseParam", "基本参数");
        writer.addHeaderAlias("allParam", "完整参数");
        writer.addHeaderAlias("unit", "单位");
        writer.addHeaderAlias("brand", "品牌");
        writer.addHeaderAlias("xinghao", "型号");

        writer.addHeaderAlias("sonsysName", "子系统名字");
        writer.addHeaderAlias("baojia", "报价");

        writer.addHeaderAlias("zhekou", "折扣");
        writer.addHeaderAlias("shijibaojia", "实际报价");

        writer.addHeaderAlias("proNum", "数量");
        writer.addHeaderAlias("allbaojia", "总报价");
        writer.addHeaderAlias("beizhu", "备注");

        return writer;
    }

    public void WriteExcelWithSonsys(int projectId, HttpServletResponse response) {
        ExcelWriter writer = getExcelWriterALL();
        //所有的统计
        List<ProductInnerDO> productInnerList = productInnerService.listall(projectId);
        List<Double> alltongji = getAlltongji(productInnerList);
        //表头信息，
        ProjectDO projectDO = projectService.get(projectId);

        int allcol = 22;

        writer.merge(allcol, projectDO.getProjectNum() + "." + projectDO.getProjectName()
                + "  成本：" + MyUtils.getAlldouble(alltongji.get(0))
                + "  报价：" + MyUtils.getAlldouble(alltongji.get(1))
                + "  利润：" + MyUtils.getAlldouble(alltongji.get(2)));
        writer.merge(allcol, "");


        List<Integer> sonsyss = getSonsysListByProjectId(projectId);
        int num = 1;
        for (Integer sonsysId : sonsyss) {
            List<ProductInnerDO> sonsysproList = getSonsysExcelVoBySonsysId(sonsysId);
            List<ProjectVo> voBydo = getVoBydo(sonsysproList);

            SonsysDO sonsysDO = sonsysService.get(sonsysId);
            System.out.println("当前子系统="+sonsysDO);
            writer.merge(allcol, sonsysDO.getSonsysName()
                    + "  成本：" + MyUtils.getAlldouble(sonsysDO.getSonchengben())
                    + "  报价：" + MyUtils.getAlldouble(sonsysDO.getSonbaojia())
                    + "  利润：" + MyUtils.getAlldouble(sonsysDO.getSonlirun()));

            if (num == 1) {
                writer.write(voBydo, true);
                num++;
            } else {
                writer.write(voBydo);
            }


        }

        setEXCELWithEnd(response, projectDO, writer);
    }

    public void setEXCELWithEnd(HttpServletResponse response, ProjectDO projectDO, ExcelWriter writer) {
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
        String s = projectDO.getProjectNum() + "." + projectDO.getProjectName();
        String name = null;
        try {
            name = name = new String(s.getBytes("GBK"), "ISO8859_1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-Disposition", "attachment;filename=" + name + ".xls");
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            writer.flush(out, true);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭writer，释放内存
            writer.close();
        }
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }


    @GetMapping("/buildsonmoney/{sonsysid}/{projectId}")
    @ResponseBody
    public R buildsonmoney(@PathVariable("sonsysid") Integer sonsysId, @PathVariable("projectId") Integer projectId) {

        SonsysDO sonsysDO = sonsysService.get(sonsysId);


        List<ProductInnerDO> productInnerDOs = productInnerService.calculateSonsys(projectId, sonsysId);

        Double sonchengben = new Double(0.000);
        Double sonbaojia = new Double(0.000);
        Double sonlirun = new Double(0.000);

        for (ProductInnerDO productInnerDO : productInnerDOs) {
            ProductDO productDO = productService.get(productInnerDO.getProductId());
            productInnerDO.setProductDO(productDO);
            double oneproductchengben = productDO.getAnzhuangChengben() + productDO.getShebeiChengben() + productDO.getChengben();

            sonchengben += oneproductchengben * productInnerDO.getProNum();

            sonbaojia += productInnerDO.getShijibaojia() * productInnerDO.getProNum();


        }
        sonlirun = (sonbaojia - sonchengben);
        sonsysDO.setSonchengben(sonchengben);
        sonsysDO.setSonbaojia(sonbaojia);
        sonsysDO.setSonlirun(sonlirun);
        sonsysDO.setSonchengben(MyUtils.getDouble2(sonsysDO.getSonchengben()));
        sonsysDO.setSonbaojia(MyUtils.getDouble2(sonsysDO.getSonbaojia()));
        sonsysDO.setSonlirun(MyUtils.getDouble2(sonsysDO.getSonlirun()));

        sonsysService.update(sonsysDO);

        return R.ok().put("sonchengben", sonsysDO.getSonchengben())
                .put("sonbaojia", sonsysDO.getSonbaojia())
                .put("sonjectlirun", sonsysDO.getSonlirun());
    }

    @RequestMapping("/downExcel/{projectId}")
    @RequiresPermissions("system:productInner:downExcel")
    public void downExcel(@PathVariable("projectId") Integer projectId, HttpServletResponse response) {
        WriteExcelWithSonsys(projectId, response);

    }

    public void WriteExcelWithSonsysNochengben(Integer projectId, HttpServletResponse response) {
        ExcelWriter writer = getExcelWriterALLNochengben();
        //所有的统计
        List<ProductInnerDO> productInnerList = productInnerService.listall(projectId);
        List<Double> alltongji = getAlltongji(productInnerList);
        //表头信息，
        ProjectDO projectDO = projectService.get(projectId);

        int allcol = 14;

        writer.merge(allcol, projectDO.getProjectNum() + "." + projectDO.getProjectName() +
                "  报价：" + MyUtils.getAlldouble(alltongji.get(1)));
        writer.merge(allcol, "");


        List<Integer> sonsyss = getSonsysListByProjectId(projectId);
        int num = 1;
        for (Integer sonsysId : sonsyss) {
            List<ProductInnerDO> sonsysproList = getSonsysExcelVoBySonsysId(sonsysId);
            List<BaojiaVo> voBydo = getBaojiaVoBydo(sonsysproList);

            SonsysDO sonsysDO = sonsysService.get(sonsysId);
            writer.merge(allcol, sonsysDO.getSonsysName()
                    + "  报价：" + MyUtils.getAlldouble(sonsysDO.getSonbaojia()));
            if (num == 1) {
                writer.write(voBydo, true);
                num++;
            } else {
                writer.write(voBydo);
            }
            System.out.println("baojiavo="+voBydo);


        }

        setEXCELWithEnd(response, projectDO, writer);
    }

    private List<BaojiaVo> getBaojiaVoBydo(List<ProductInnerDO> sonsysproList) {
        ArrayList<BaojiaVo> baojiaVos = new ArrayList<>();

        for (ProductInnerDO productInnerDO : sonsysproList) {
            BaojiaVo baojiaVo = new BaojiaVo();
            ProductDO productDO2 = productService.get(productInnerDO.getProductId());
            productInnerDO.setProductDO(productDO2);

            SonsysDO sonsysDO = sonsysService.get(productInnerDO.getSonsysId());
            productInnerDO.setSonsysDO(sonsysDO);

            CategoryDO categoryDO = categoryService.get(productDO2.getCategoryId());
            productDO2.setCategoryDO(categoryDO);
            BeanUtils.copyProperties(productInnerDO, baojiaVo);
            baojiaVo.setCategoryName(categoryDO.getCategoryName());

            System.out.println("报价查出来的分类名字"+baojiaVo.getCategoryName());

            BeanUtils.copyProperties(productDO2,baojiaVo);

            baojiaVo.setShebeiName(productInnerDO.getProductDO().getShebeiName());
            baojiaVo.setSonsysName(productInnerDO.getSonsysDO().getSonsysName());
            baojiaVo.setCategoryName(categoryDO.getCategoryName());
            baojiaVos.add(baojiaVo);
        }

        return baojiaVos;
    }

    @RequestMapping("/downExcel2/{projectId}")
    @RequiresPermissions("system:productInner:downExcel2") //报价的excel
    public void downExcel2(@PathVariable("projectId") Integer projectId, HttpServletResponse response) {
        WriteExcelWithSonsysNochengben(projectId, response);

    }

    @GetMapping("/{projectId}/{sonsysId}")
    @RequiresPermissions("system:productInner:productInner")
    String ProductInner(@PathVariable("projectId") Integer projectId, Model model,
                        HttpSession session, HttpServletRequest request
            , @PathVariable("sonsysId") Integer sonsysId) {

        SonsysDO sonsysDO = sonsysService.get(sonsysId);


        List<ProductInnerDO> productInnerDOs = productInnerService.calculateSonsys(projectId, sonsysId);

        Double sonchengben = new Double(0.000);
        Double sonbaojia = new Double(0.000);
        Double sonlirun = new Double(0.000);

        for (ProductInnerDO productInnerDO : productInnerDOs) {
            ProductDO productDO = productService.get(productInnerDO.getProductId());
            productInnerDO.setProductDO(productDO);
            double oneproductchengben = productDO.getAnzhuangChengben() + productDO.getShebeiChengben() + productDO.getChengben();
            sonchengben += oneproductchengben * productInnerDO.getProNum();

            sonbaojia += oneproductchengben* 2 * productInnerDO.getZhekou()*productInnerDO.getProNum();


        }
        sonlirun = (sonbaojia - sonchengben);
        sonsysDO.setSonchengben(sonchengben);
        sonsysDO.setSonbaojia(sonbaojia);
        sonsysDO.setSonlirun(sonlirun);
        sonsysDO.setSonchengben(MyUtils.getDouble2(sonsysDO.getSonchengben()));
        sonsysDO.setSonbaojia(MyUtils.getDouble2(sonsysDO.getSonbaojia()));
        sonsysDO.setSonlirun(MyUtils.getDouble2(sonsysDO.getSonlirun()));

        sonsysService.update(sonsysDO);

        model.addAttribute("sonsysDO", sonsysDO);
        model.addAttribute("projectId", projectId);
        model.addAttribute("project", projectService.get(projectId));

        return "system/productInner/productInner";
    }

    public List<Double> getAlltongji(List<ProductInnerDO> productInnerList) {
        Double projectchengben = new Double(0.00);
        Double projectbaojia = new Double(0.00);
        Double projectlirun = new Double(0.00);


        for (ProductInnerDO productInnerDO : productInnerList) {
            projectchengben = projectchengben + productInnerDO.getAllchengben();
            projectbaojia = projectbaojia + productInnerDO.getAllbaojia();
            projectlirun = projectlirun + productInnerDO.getAlllirun();
            System.out.println("成本=" + projectchengben + "报价=" + projectbaojia + "利润=" + projectlirun);
        }
        projectchengben = MyUtils.getDouble2(projectchengben);
        projectbaojia = MyUtils.getDouble2(projectbaojia);
        projectlirun = MyUtils.getDouble2(projectlirun);

        ArrayList<Double> list = new ArrayList<>();
        list.add(projectchengben);
        list.add(projectbaojia);
        list.add(projectlirun);

        return list;
    }

    @ResponseBody
    @GetMapping("/buildmoney/{projectId}")
    public R buildmoney(@PathVariable("projectId") Integer projectId) {
        List<ProductInnerDO> productInnerList = productInnerService.projectAllMoney(projectId);
        Double projectchengben = new Double(0.00);
        Double projectbaojia = new Double(0.00);
        Double projectlirun = new Double(0.00);


        for (ProductInnerDO productInnerDO : productInnerList) {

            ProductDO productDO = productService.get(productInnerDO.getProductId());
            double onechengben = productDO.getAnzhuangChengben() + productDO.getChengben() + productDO.getShebeiChengben();

            double allchengben = onechengben * productInnerDO.getProNum();
            projectchengben = projectchengben + allchengben;


            double baojiayibei = onechengben*2*productInnerDO.getZhekou();
            double allbaojia = baojiayibei * productInnerDO.getProNum();
            projectbaojia = projectbaojia + allbaojia;




            System.out.println("成本=" + projectchengben + "报价=" + projectbaojia + "利润=" + projectlirun);
        }
        projectlirun = projectbaojia-projectchengben;

        projectchengben = MyUtils.getDouble2(projectchengben);
        projectbaojia = MyUtils.getDouble2(projectbaojia);
        projectlirun = MyUtils.getDouble2(projectlirun);
        return R.ok().put("projectchengben", MyUtils.getAlldouble(projectchengben))
                .put("projectbaojia", MyUtils.getAlldouble(projectbaojia))
                .put("projectlirun", MyUtils.getAlldouble(projectlirun));
    }

    public void calall(HttpServletRequest request, Integer projectId) {

        List<ProductInnerDO> productInnerList = productInnerService.projectAllMoney(projectId);
        Double projectchengben = new Double(0.00);
        Double projectbaojia = new Double(0.00);
        Double projectlirun = new Double(0.00);


        for (ProductInnerDO productInnerDO : productInnerList) {
            ProductDO productDO = productService.get(productInnerDO.getProductId());

            double onechengben = productDO.getAnzhuangChengben() + productDO.getChengben() + productDO.getShebeiChengben();
            double chengben = onechengben  * productInnerDO.getProNum();
            projectchengben = projectchengben+chengben;

            double baojia = onechengben * 2 * productInnerDO.getZhekou() * productInnerDO.getProNum();
            projectbaojia = projectbaojia+baojia;
        }
        projectchengben = MyUtils.getDouble2(projectchengben);
        projectbaojia = MyUtils.getDouble2(projectbaojia);
        projectlirun = MyUtils.getDouble2(projectbaojia-projectchengben);

        request.setAttribute("projectchengben", MyUtils.getAlldouble(projectchengben));
        request.setAttribute("projectbaojia", MyUtils.getAlldouble(projectbaojia));
        request.setAttribute("projectlirun", MyUtils.getAlldouble(projectlirun));

    }


    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("system:productInner:productInner")
    public PageUtils list(@RequestParam Map<String, Object> params, HttpSession session) {
        Integer projectId = (Integer) session.getAttribute("projectId");
        System.out.println("从session的得到的项目id=" + projectId);
        params.put("projectId", projectId);
        //查询列表数据
        Query query = new Query(params);
        List<ProductInnerDO> productInnerList = productInnerService.list(query);
        int total = productInnerService.count(query);
        PageUtils pageUtils = new PageUtils(productInnerList, total);

        return pageUtils;
    }

    @GetMapping("/add/{projectId}/{sonsysId}")
    @RequiresPermissions("system:productInner:add")
    String add(@PathVariable("projectId") Integer projectId, Model model, HttpServletRequest request,
               @PathVariable("sonsysId") Integer sonsysId) {

        List<ProductDO> products = productService.listall();

        List<CategoryDO> categoryList = categoryService.list(new HashMap<String, Object>());

        model.addAttribute("categoryList", categoryList);


        model.addAttribute("products", products);

        System.out.println("所有的商品有：" + products.size());

        model.addAttribute("projectId", projectId);
        model.addAttribute("sonsysId", sonsysId);

        return "system/productInner/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("system:productInner:edit")
    String edit(@PathVariable("id") Integer id, Model model, HttpServletRequest request) {
        ProductInnerDO productInner = productInnerService.get(id);

        SonsysDO sonsysDO = sonsysService.get(productInner.getSonsysId());

        productInner.setSonsysDO(sonsysDO);

        model.addAttribute("productInner", productInner);

        String username = request.getSession().getAttribute("username").toString();
        UserDO userDO = userService.getUserByUsername(username);
        List<SonsysDO> sonsyslist = sonsysService.listforchoose(userDO.getUserId());

        //子系统list
        model.addAttribute("sonsyslist", sonsyslist);


        return "system/productInner/edit";
    }

    /**
     * 保存
     */
    @Transactional
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("system:productInner:add")
    public R save(ProductInnerDO productInner, HttpSession session, HttpServletRequest request) {

        if (productInner.getProductId() == null || productInner.getSonsysId() == null) {
            return R.error(444, "产品或子系统为空");
        }
        Integer productId = productInner.getProductId();

        ProductDO productDO = productService.get(productId);
        if(productDO==null){
            return R.error(444,"输入的产品id不存在");
        }



        System.out.println("项目=" + productInner.getProjectId() + " 产品" + productInner.getProductId());

        productInner.setProductDO(productDO);

        productInner = MyUtils.buildMoney(productInner);

        //设置一下两倍的报价
        double onechengben = productDO.getAnzhuangChengben() + productDO.getShebeiChengben() + productDO.getChengben();

        productInner.setBaojia(onechengben*2);
        //折扣*报价   设置一下实际报价
        double shijibaojia = productInner.getBaojia() * productInner.getZhekou();
        productInner.setShijibaojia(shijibaojia);

        if (productInnerService.save(productInner) > 0) {
            getSonsysMoney(productInner.getSonsysId());
            return R.ok().put("projectIdforfresh", productInner.getProjectId());
        }


        return R.error();
    }


    public SonsysDO setSonsysMoney(ProductInnerDO productInnerDO) {
        SonsysDO sonsysDO = new SonsysDO();
        sonsysDO.setSonsysId(productInnerDO.getSonsysId());

        sonsysDO.setSonchengben(productInnerDO.getAllchengben());
        sonsysDO.setSonbaojia(productInnerDO.getAllbaojia());
        sonsysDO.setSonlirun(productInnerDO.getAlllirun());

        sonsysService.update(sonsysDO);

        return sonsysDO;
    }

    @GetMapping("/buildsonmoney/{sonsysId}")
    public R buildsonmoney(@PathVariable("sonsysId") Integer sonsysId) {

        ProductInnerDO productInnerDO = getSonsysMoney(sonsysId);

        SonsysDO sonsysDO = setSonsysMoney(productInnerDO);
        sonsysDO.setSonchengben(MyUtils.getDouble2(sonsysDO.getSonchengben()));
        sonsysDO.setSonbaojia(MyUtils.getDouble2(sonsysDO.getSonbaojia()));
        sonsysDO.setSonlirun(MyUtils.getDouble2(sonsysDO.getSonlirun()));


        return R.ok().put("sonchengben", sonsysDO.getSonchengben())
                .put("sonbaojia", sonsysDO.getSonbaojia())
                .put("sonjectlirun", sonsysDO.getSonlirun());
    }

    public ProductInnerDO getSonsysMoney(int sonsysId) {
        ProductInnerDO productInnerDO1 = new ProductInnerDO();
        productInnerDO1.setSonsysId(sonsysId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("sonsysId", sonsysId);
        List<ProductInnerDO> list = productInnerService.list(map);
        for (ProductInnerDO productInnerDO : list) {
            productInnerDO1.setAllchengben(productInnerDO1.getAllchengben() + productInnerDO.getAllchengben());
            productInnerDO1.setAllbaojia(productInnerDO1.getAllbaojia() + productInnerDO.getAllbaojia());
            productInnerDO1.setAlllirun(productInnerDO1.getAlllirun() + productInnerDO.getAlllirun());
        }
        setSonsysMoney(productInnerDO1);

        return productInnerDO1;

    }

    /**
     * 修改
     */
    @Transactional
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("system:productInner:edit")
    public R update(ProductInnerDO productInner) {



        ProductInnerDO productInner2 = productInnerService.get(productInner.getId());

        productInnerService.update(productInner);

        if(productInner2.getZhekou()==productInner.getZhekou()){

        }else {


            double onechenben = productInner2.getProductDO().getAnzhuangChengben() + productInner2.getProductDO().getShebeiChengben() + productInner2.getProductDO().getChengben();
            System.out.println("一个产品成本=" + onechenben + "  所有=" + productInner.getProNum() * onechenben);
            productInner.setAllchengben(productInner.getProNum() * onechenben);

            productInner.setAllbaojia(productInner.getBaojia() * productInner.getProNum());

            productInner.setAlllirun(productInner.getAllbaojia() - productInner.getAllchengben());

            productInner.setProfit(productInner.getAlllirun() / productInner.getAllchengben() * 100);
            System.out.println("edit的productInner" + productInner);

            double shijibaojia = productInner.getBaojia() * productInner.getZhekou();

            productInner.setShijibaojia(MyUtils.getDouble2(shijibaojia));
            //编辑这个报价，这个系统的都变化

            ProductDO productDO = productService.get(productInner.getProductId());
            double onechengben = productDO.getAnzhuangChengben() + productDO.getShebeiChengben() + productDO.getChengben();

            Double baojia = onechengben * 2;

            productInner.setBaojia(MyUtils.getDouble2(baojia));

            shijibaojia = baojia * productInner.getZhekou();

            productInner.setShijibaojia(MyUtils.getDouble2(shijibaojia));

            productInner.setAllbaojia(MyUtils.getDouble2(shijibaojia * productInner.getProNum()));

            productInner.setAllchengben(MyUtils.getDouble2(onechenben * productInner.getProNum()));


            productInner.setAlllirun(MyUtils.getDouble2(productInner.getAllbaojia() - productInner.getAllchengben()));

            productInner.setProfit(MyUtils.getDouble2(productInner.getAlllirun() / productInner.getAllchengben() * 100));


            productInnerService.updateAllProductWithProjectId(productInner);
        }
       // productInnerService.update(productInner);

        getSonsysMoney(productInner.getSonsysId());
        return R.ok().put("projectIdforfresh", productInner.getProjectId());
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("system:productInner:remove")
    public R remove(Integer id) {
        ProductInnerDO productInnerDO = productInnerService.get(id);
        if (productInnerService.remove(id) > 0) {

            getSonsysMoney(productInnerDO.getSonsysId());
            return R.ok().put("sonsysIdforfresh", productInnerDO.getSonsysId()).put("projectIdforfresh", productInnerDO.getProjectId());
        }
        return R.error();
    }

    /**
     * 删除
     */
    @Transactional
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("system:productInner:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {

        int sonsysId = 0;
        Integer projectIdforfresh = 1;
        for (Integer id : ids) {
            ProductInnerDO productInnerDO = productInnerService.get(id);
            productInnerService.remove(id);
            getSonsysMoney(productInnerDO.getSonsysId());
            sonsysId = productInnerDO.getSonsysId();
            projectIdforfresh = productInnerDO.getProjectId();
        }

        return R.ok().put("sonsysIdforfresh", sonsysId).put("projectIdforfresh", projectIdforfresh);
    }

}
